<?php
/**
 * 模块: 权限菜单管理
 * User: 文丹
 * Date: 2018/1/23
 * Time: 16:18
 */
namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;
class Menu extends Power
{
    /**
     *  菜单列表
     */
    public function index(){

        $items  = model('Item')->selectItems();
        $menu   = model('Menu')->select();
        $count  = model('Menu')->count();
        $this->assign([
            'menu'=>$menu,
            'count'=>$count,
            'items'=>$items,
            'empty'=>'<tr class="text-c va-m empty"><td colspan="7">暂无数据</td></tr>'
        ]);
        return $this->fetch('menu:index');
    }
    /**
     *  菜单名验证
     */
    public function checkName(){
        $name = trim(input('name'));
        $result = model('Menu')->checkName($name);
        if($result){
            return 1;
        }else{
            return 2;
        }
    }
    /**
     *  地址验证
     */
    public function checkUrl(){
        $url = strtolower(trim(input('url')));
        $result = model('Menu')->checkUrl($url);
        if($result){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     *  菜单添加
     */
    public function menuAdd()
    {
        $url     = strtolower(trim(input('url')));
        $name    = trim(input('name'));
        $item_id = intval(input('item_id'));
        // $url;die;
        if (empty($url) || empty($name)||empty($item_id)) {
            return json(['msg'=>'请填写完整的数据','ok'=>false]);
        }
        if (!empty($url) && !empty($name)&& !empty($item_id)) {
            $data = ['name' => $name, 'url' => $url,'item_id'=>$item_id];
            $result = model('Menu')->insert($data);
            if ($result) {
                return json(['msg'=>'添加成功','ok'=>true]);
            } else {
                return json(['msg'=>'添加失败','ok'=>false]);
            }
        }
    }

    /**
     *  菜单 - 停用
     */
    public function menuStop()
    {
        $id = intval(input('id'));
        $row = model('Menu')->menuStop($id);
        if ($row > 0) {
            return 1;
        } else {
            return 2;
        }
    }

    /**
     * 菜单 - 启用
     */
    public function menuStart()
    {
        $id = intval(input('id'));
        $row = model('Menu')->menuStart($id);
        if ($row > 0) {
            return 1;
        } else {
            return 2;
        }
    }

    /**
     *  权限菜单 - 删除
     */
    public function menuDel()
    {
        $id = intval(input('id'));
        $row = model('Menu')->menuDel($id);
        if ($row > 0) {
            return 1;
        } else {
            return 2;
        }
    }
    public function checkNameX(){
        $name = trim(input('name'));
        $menu_id = intval(input('menu_id'));
        $result = model('Menu')->checkNameX($name,$menu_id);
        if($result){
            return 1;
        }else{
            return 2;
        }
    }
    public function checkUrlX(){
        $url = strtolower(trim(input('url')));
        $menu_id = intval(input('menu_id'));
        $result = model('Menu')->checkUrlX($url,$menu_id);
        if($result){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     *  权限菜单 - 修改保存
     */
    public function menuEdit(){
        $id     = intval(input('id'));
        $url    = strtolower(trim(input('url')));
        $name   = trim(input('name'));
        $itemId = intval(input('itemId'));
        $checkUrl = model('Menu')->checkUrlX($url,$id);
        $checkName = model('Menu')->checkNameX($name,$id);
        if($checkUrl || $checkName){
           return json(['msg'=>'权限名称或者url地址已经存在！','ok'=>false]);
        }
        if (empty($url) || empty($name)||empty($itemId)) {
            return json(['msg'=>'请填写完整的数据！','ok'=>false]);
        }
        if (!empty($url) && !empty($name)&& !empty($itemId)) {
            $data = ['name' => $name, 'url' => $url,'item_id'=>$itemId];
            $result = model('Menu')->menuEdit($data,$id);
            if($result>0) {
                return json(['msg'=>'修改成功！','ok'=>true]);
            } else {
                return json(['msg'=>'修改失败！','ok'=>false]);
            }
        }
    }

    /**
     *  菜单 - 搜索
     */
    public function search(){
        $change    = intval(input('change'));
        $item_id   = intval(input('item_id'));
        $url       = strtolower(trim(input('url')));
        $name      = trim(input('name'));
        $items     = model('Item')->selectItems();
        $item_name = model('Item')->getItemName($item_id);
        //$item_name = trim(input('item_name'));
        if($change==1){
            //按栏目搜索
            $result = model('Menu')->searchItemId($item_id);
            $count  = model('Menu')->countItemId($item_id);
           // print_r($result);
            if(!empty($result[0])){
                $this->assign('menu', $result);
                $this->assign('count', $count);
                $this->assign('items', $items);
                return $this->fetch('menu:index',['change'=>$change,'item_id'=>$item_id,'url'=>$url,'name'=>$name,'item_name'=>$item_name]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false,'code'=>502]);
            }
        }
        if($change==2){
            //按菜单名 搜索
            if(empty($name)){
                return json(['msg'=>'搜索条件不能为空','ok'=>false,'code'=>501]);
            }
            $result = model('Menu')->searchName($name);
            $count  = model('Menu')->countName($name);
           // print_r($result);
            if(!empty($result[0])){
                $this->assign('menu', $result);
                $this->assign('count', $count);
                $this->assign('items', $items);
                return $this->fetch('menu:index',['change'=>$change,'item_id'=>$item_id,'url'=>$url,'name'=>$name,'item_name'=>$item_name]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false,'code'=>502]);
            }
        }
        if($change==3){
            //按菜单地址搜索
            if(empty($url)){
                return json(['msg'=>'搜索条件不能为空','ok'=>false,'code'=>501]);
            }
            $result = model('Menu')->searchUrl($url);
            $count  = model('Menu')->countUrl($url);
          //  print_r($result);
            if(!empty($result[0])){
                $this->assign('menu', $result);
                $this->assign('count', $count);
                $this->assign('items', $items);
                return $this->fetch('menu:index',['change'=>$change,'item_id'=>$item_id,'url'=>$url,'name'=>$name,'item_name'=>$item_name]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false,'code'=>502]);
            }
        }
    }
}

